package com.css.bjdt_report.report.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONException;
import org.json.JSONObject;
import org.nutz.dao.sql.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.css.bjdt_report.cell.entity.Cell;
import com.css.bjdt_report.cell.service.CellService;
import com.css.bjdt_report.report.entity.RptTableValidationFormula;
import com.css.bjdt_report.report.entity.RptTemplate;
import com.css.bjdt_report.report.entity.RptTemplateValidation;
import com.css.bjdt_report.report.entity.RptTemplateValidation.ValidationType;
import com.css.bjdt_report.report.service.ReportTemplateManageService;
import com.css.bjdt_report.report.service.ReportTemplateValidationService;
import com.css.bjdt_report.util.CommonUtil;
import com.css.bjdt_report.util.JsonResult;
import com.css.bjdt_report.util.PropertyFilter;
import com.css.bjdt_report.validation.service.ReportValidationFormulaOnloadService;

//报表校核
@Controller
@RequestMapping(value = "/reportTemplateValidationController")
public class ReportTemplateValidationController {

	@Autowired
	private ReportTemplateValidationService reportTemplateValidationService;
	@Autowired
	private ReportTemplateManageService reportTemplateManageService;
	@Autowired
	private CellService cellService;
	@Autowired
	private ReportValidationFormulaOnloadService reportValidationFormulaOnloadService;

	//行校核公式转换
	public List<String> rowValidationChange(String validationFormulaRow) {
		//先去掉所有空格
		validationFormulaRow = validationFormulaRow.replace(" ", "");

		//先判断是否有波浪线，如果有波浪线，说明是从多少行到多少行，例如2~5，要转换为2行+3行+4行+5行
		if (validationFormulaRow.indexOf("~") > 0) {
			String waveString[] = validationFormulaRow.split("~");
			for (int a = 0; a < waveString.length; a++) {
				if (a + 1 == waveString.length) {
					break;
				}
				String startStr = getNumStart(waveString[a]);
				String endStr = getNumEnd(waveString[a + 1]);
				//算出开始和结束的差值，并进行循环，补全破浪线中的数字
				String middleStr = "";
				for (int b = Integer.parseInt(startStr); b < Integer.parseInt(endStr) + 1; b++) {
					middleStr += b + "+";
				}
				validationFormulaRow = validationFormulaRow.replace(startStr + "~" + endStr,
						middleStr.substring(0, middleStr.length() - 1));
			}
		}

		String[] validation = validationFormulaRow.split("[\\=\\>\\</]");
		//去掉数组中的空格元素
		List<String> strList = new ArrayList<String>();
		for (String str : validation) {
			str = str.replace(" ", "");
			if (str != null && str.length() > 0) {
				strList.add(str);
			}
		}
		//取到数组中的每一个元素，然后加上行，进行替换
		List<String> dataList = new ArrayList<String>();
		if (strList.size() == 2) {
			String[] validationNum = validationFormulaRow.split("[\\+\\-\\*\\=\\>\\</]");
			String[] array = validationFormulaRow.split("[0-9]");

			List<String> tmp = new ArrayList<String>();
			List<String> tmp1 = new ArrayList<String>();
			String ss = "";
			for (String str : array) {
				if (str != null && str.length() != 0) {
					tmp.add(str);
				}
			}
			for (String str : validationNum) {
				if (str != null && str.length() != 0) {
					tmp1.add(str);
				}
			}
			for (int i = 0; i < tmp1.size(); i++) {
				if (i == 0) {
					ss = tmp1.get(i) + "行" + tmp.get(i);
					continue;
				}
				if (i < tmp.size()) {
					ss += tmp1.get(i) + "行" + tmp.get(i);
				} else {
					ss += tmp1.get(i) + "行";
				}
			}

			dataList.add(ss);
			if (!(ss.contains(">") || ss.contains("<"))) {
				dataList.add(ss.replaceFirst("=", "=="));
			} else {
				dataList.add(ss);
			}
			return dataList;
		} else {
			dataList.add("公式错误");
			return dataList;
		}
	}

	public String getNumStart(String formula) {
		String[] array = formula.split("[\\+\\-\\*\\=\\>\\</]");
		int a = array.length;
		return array[a - 1];
	}

	public String getNumEnd(String formula) {
		String[] array = formula.split("[\\+\\-\\*\\=\\>\\</]");
		return array[0];
	}

	//列校核公式转换
	public List<String> colValidationChange(@PathVariable String validationFormulaCol) {
		//先去掉所有空格
		validationFormulaCol = validationFormulaCol.replace(" ", "");
		String[] colArray = validationFormulaCol.split("[\\+\\-\\*\\=\\>\\<\\~/]");
		//先把公式中的字母转化成数字
		for (String str : colArray) {
			str = str.replace(" ", "");
			if (str != null && str.length() > 0) {
				validationFormulaCol = validationFormulaCol.replaceFirst(str, CommonUtil.letterToNum(str));
			}
		}
		//先判断是否有波浪线，如果有波浪线，说明是从多少列到多少列，例如2~5，要转换为2列+3列+4列+5列
		if (validationFormulaCol.indexOf("~") > 0) {
			String waveString[] = validationFormulaCol.split("~");
			for (int a = 0; a < waveString.length; a++) {
				if (a + 1 == waveString.length) {
					break;
				}
				String startStr = getNumStart(waveString[a]);
				String endStr = getNumEnd(waveString[a + 1]);
				//算出开始和结束的差值，并进行循环，补全破浪线中的数字
				String middleStr = "";
				for (int b = Integer.parseInt(startStr); b < Integer.parseInt(endStr) + 1; b++) {
					middleStr += b + "+";
				}
				validationFormulaCol = validationFormulaCol.replace(startStr + "~" + endStr,
						middleStr.substring(0, middleStr.length() - 1));
			}
		}
		//根据公式，将1列=2列+3列 转换成A列=B列+C列
		String[] validationLetterArray = validationFormulaCol.split("[\\+\\-\\*\\=\\>\\</]");
		String validationFormulaColLetter = validationFormulaCol;
		for (String str : validationLetterArray) {
			str = str.replace(" ", "");
			if (str != null && str.length() > 0) {
				validationFormulaColLetter = validationFormulaColLetter.replaceFirst(str, CommonUtil.numToLetter(str));
			}
		}

		String[] validation = validationFormulaCol.split("[\\=\\>\\</]");
		//去掉数组中的空格元素
		List<String> strList = new ArrayList<String>();
		for (String str : validation) {
			str = str.replace(" ", "");
			if (str != null && str.length() > 0) {
				strList.add(str);
			}
		}
		//取到数组中的每一个元素，然后加上列，进行替换
		List<String> dataList = new ArrayList<String>();
		if (strList.size() == 2) {
			String[] validationNum = validationFormulaCol.split("[\\+\\-\\*\\=\\>\\</]");
			for (String str : validationNum) {
				str = str.replace(" ", "");
				if (str != null && str.length() > 0) {
					validationFormulaCol = validationFormulaCol.replaceFirst(str, str + "列");
				}
			}
			if (!(validationFormulaCol.contains(">") || validationFormulaCol.contains("<"))) {
				dataList.add(validationFormulaCol.replaceFirst("=", "=="));
			} else {
				dataList.add(validationFormulaCol);
			}
			dataList.add(validationFormulaColLetter);
			return dataList;
		} else {
			dataList.add("公式错误");
			return dataList;
		}
	}

	//将单元格转换成指定的公式，例如A1，转换成[1_1]，B1，就转换成[2_1]
	public String cellToNum(String cellStr) {
		String numStr = cellStr.replaceAll("[a-zA-Z]", "");
		String letterStr = cellStr.replaceAll("[0-9]", "");
		String letterNumStr = CommonUtil.letterToNum(letterStr);
		if (numStr.equals("") || letterStr.equals("")) {
			return "0";
		} else {
			return "[" + letterNumStr + "_" + numStr + "]";
		}
	}

	//将单元格校核式进行转换，例如A1=B1+C1，转换为单元格ID相加;
	public List<String> cellValidationChange(String validationFormulaCell, String templateId) {
		String validationFormulaCellShow = validationFormulaCell;
		//先去掉所有空格
		validationFormulaCell = validationFormulaCell.replace(" ", "");
		List<String> dataList = new ArrayList<String>();
		if (validationFormulaCell.indexOf("~") > 0) {
			dataList.add("公式错误");
			return dataList;
		} else {
			String[] validation = validationFormulaCell.split("[\\=\\>\\</]");
			//去掉数组中的空格元素
			List<String> strList = new ArrayList<String>();
			for (String str : validation) {
				str = str.replace(" ", "");
				if (str != null && str.length() > 0) {
					strList.add(str);
				}
			}
			if (strList.size() == 2) {
				String[] cellArray = validationFormulaCell.split("[\\+\\-\\*\\=\\>\\</]");
				boolean isTrue = true;
				for (String cellStr : cellArray) {
					cellStr = cellStr.replace(" ", "");
					if (cellStr != null && cellStr.length() > 0) {
						if (cellToNum(cellStr).equals("0")) {
							isTrue = false;
							break;
						} else {
							String numStr = cellStr.replaceAll("[a-zA-Z]", "");
							String letterStr = cellStr.replaceAll("[0-9]", "");
							String letterNumStr = CommonUtil.letterToNum(letterStr);
							//获得当前这个坐标的CELLID
							Cell cell = cellService.getCellByTemplateIdRowCol(templateId, numStr, letterNumStr);
							if (cell == null) {
								isTrue = false;
								break;
							}
							validationFormulaCell = validationFormulaCell.replaceFirst(cellStr, cell.getCellId());
						}
					}
				}
				if (isTrue) {
					if (!(validationFormulaCell.contains(">") || validationFormulaCell.contains("<"))) {
						dataList.add(validationFormulaCell.replaceFirst("=", "=="));
					} else {
						dataList.add(validationFormulaCell);
					}
					dataList.add(validationFormulaCellShow.toUpperCase());
				} else {
					dataList.add("公式错误");
				}
				return dataList;
			} else {
				dataList.add("公式错误");
				return dataList;
			}
		}
	}

	//传进入行校核的JSON，解析成LIST
	public List<RptTemplateValidation> rowValidationJson(String rowValidationFormula, String templateId) {
		try {
			List<RptTemplateValidation> rptTemplateValidationList = new ArrayList<RptTemplateValidation>();
			JsonResult jsonResult = new JsonResult();
			Map<String, String> map = jsonResult.jsonToString(rowValidationFormula);
			Set<String> key = map.keySet();
			for (String strKey : key) {
				String strJson = "{" + map.get(strKey) + "}";
				JSONObject jobj = new JSONObject(strJson);
				RptTemplateValidation rptTemplateValidation = new RptTemplateValidation();
				rptTemplateValidation.setTemplateId(templateId);
				rptTemplateValidation.setValidationFormula(jobj.getString("rowGongshi"));
				rptTemplateValidation.setValidationType(jobj.getString("jiaohetype"));
				rptTemplateValidation.setValidationFormulaShow(jobj.getString("rowGongshiShow"));
				rptTemplateValidationList.add(rptTemplateValidation);
			}
			return rptTemplateValidationList;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	//传进入列校核的JSON，解析成LIST
	public List<RptTemplateValidation> colValidationJson(String colValidationFormula, String templateId) {
		try {
			List<RptTemplateValidation> rptTemplateValidationList = new ArrayList<RptTemplateValidation>();
			JsonResult jsonResult = new JsonResult();
			Map<String, String> map = jsonResult.jsonToString(colValidationFormula);
			Set<String> key = map.keySet();
			for (String strKey : key) {
				String strJson = "{" + map.get(strKey) + "}";
				JSONObject jobj = new JSONObject(strJson);
				RptTemplateValidation rptTemplateValidation = new RptTemplateValidation();
				rptTemplateValidation.setTemplateId(templateId);
				rptTemplateValidation.setValidationFormula(jobj.getString("colGongshiReal"));
				rptTemplateValidation.setValidationType(jobj.getString("jiaohetype"));
				rptTemplateValidation.setValidationFormulaShow(jobj.getString("colGongshiShow"));
				rptTemplateValidationList.add(rptTemplateValidation);
			}
			return rptTemplateValidationList;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	//传进入单元格校核的JSON，解析成LIST
	public List<RptTemplateValidation> cellValidationJson(String cellValidationFormula, String templateId) {
		try {
			List<RptTemplateValidation> rptTemplateValidationList = new ArrayList<RptTemplateValidation>();
			JsonResult jsonResult = new JsonResult();
			Map<String, String> map = jsonResult.jsonToString(cellValidationFormula);
			Set<String> key = map.keySet();
			for (String strKey : key) {
				String strJson = "{" + map.get(strKey) + "}";
				JSONObject jobj = new JSONObject(strJson);
				RptTemplateValidation rptTemplateValidation = new RptTemplateValidation();
				rptTemplateValidation.setTemplateId(templateId);
				rptTemplateValidation.setValidationFormula(jobj.getString("cellGongshiReal"));
				rptTemplateValidation.setValidationType(jobj.getString("jiaohetype"));
				rptTemplateValidation.setValidationFormulaShow(jobj.getString("cellGongshiShow"));
				rptTemplateValidationList.add(rptTemplateValidation);
			}
			return rptTemplateValidationList;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	//传进入表间校核的JSON，解析成LIST
	public List<RptTemplateValidation> tableValidationJson(String tableValidationFormula, String templateId) {
		try {
			List<RptTemplateValidation> rptTemplateValidationList = new ArrayList<RptTemplateValidation>();
			JsonResult jsonResult = new JsonResult();
			Map<String, String> map = jsonResult.jsonToString(tableValidationFormula);
			Set<String> key = map.keySet();
			for (String strKey : key) {
				String strJson = "{" + map.get(strKey) + "}";
				JSONObject jobj = new JSONObject(strJson);
				RptTemplateValidation rptTemplateValidation = new RptTemplateValidation();
				rptTemplateValidation.setTemplateId(templateId);
				rptTemplateValidation.setValidationFormula(jobj.getString("tableGongshiReal"));
				rptTemplateValidation.setValidationType(jobj.getString("jiaohetype"));
				rptTemplateValidation.setValidationFormulaShow(jobj.getString("tableGongshiShow"));
				rptTemplateValidationList.add(rptTemplateValidation);
			}
			return rptTemplateValidationList;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	//报表校核式的保存
	@Transactional
	@ResponseBody
	@RequestMapping(value = "/saveValidationFormula/{templateId}", method = RequestMethod.POST)
	public void saveCountFormula(@PathVariable String templateId,
			@RequestParam(value = "rowValidationFormula") String rowValidationFormula,
			@RequestParam(value = "colValidationFormula") String colValidationFormula,
			@RequestParam(value = "cellValidationFormula") String cellValidationFormula,
			@RequestParam(value = "tableValidationFormula") String tableValidationFormula) {
		//删除校核式中所有表内校核的方法
		reportTemplateValidationService.deleteValidationByTemplateId(templateId, "1");
		reportTemplateValidationService.deleteValidationByTemplateId(templateId, "2");
		reportTemplateValidationService.deleteValidationByTemplateId(templateId, "3");
		reportTemplateValidationService.deleteValidationByTemplateId(templateId, "4");
		//将行校核、列校核、单元格校核、表间校核的json转换成LIST
		List<RptTemplateValidation> rowRptTemplateValidationList = rowValidationJson(rowValidationFormula, templateId);
		List<RptTemplateValidation> colRptTemplateValidationList = colValidationJson(colValidationFormula, templateId);
		List<RptTemplateValidation> cellRptTemplateValidationList = cellValidationJson(cellValidationFormula,
				templateId);
		List<RptTemplateValidation> tableRptTemplateValidationList = tableValidationJson(tableValidationFormula,
				templateId);
		//保存行校核、列校核、单元格校核、表间校核
		reportTemplateValidationService.insertList(rowRptTemplateValidationList);
		reportTemplateValidationService.insertList(colRptTemplateValidationList);
		reportTemplateValidationService.insertList(cellRptTemplateValidationList);
		reportTemplateValidationService.insertList(tableRptTemplateValidationList);
	}

	//根据报表的ID获得该报表的所有表内校核公式
	//报表校核式的保存
	@ResponseBody
	@RequestMapping(value = "/getAllTemplateInValidationFormula/{templateId}", method = RequestMethod.POST)
	public List<RptTemplateValidation> getAllTemplateInValidationFormula(@PathVariable String templateId) {
		return reportTemplateValidationService.getAllTemplateInValidation(templateId);
	}

	//表间校核公式转换
	@ResponseBody
	@RequestMapping(value = "/getATableValidationFormula/{templateId}", method = RequestMethod.POST)
	public void getATableValidationFormula(@PathVariable String templateId,
			@RequestParam(value = "leftTableValidationFormulaStr") String leftTableValidationFormulaStr,
			@RequestParam(value = "valudationFormulaSymbol") String valudationFormulaSymbol,
			@RequestParam(value = "templateSelectValueStr") String templateSelectValueStr,
			@RequestParam(value = "expressionTextValueStr") String expressionTextValueStr,
			@RequestParam(value = "relationSelectName") String relationSelectName) {
		String[] templateSelectValueArray = templateSelectValueStr.split(",");
		String[] expressionTextValueArray = expressionTextValueStr.split(",");
		//先判断公式中间是相加还是分别,相加的话，是一个公式，如果是分别的话，是一堆公式
		if (relationSelectName.equals("1")) {
			String leftFormulaShow = resolveValidationFormulaLeft(leftTableValidationFormulaStr);
			String leftFormulaReal = getCellId(templateId, leftFormulaShow);
			String formulaShow = "";
			String formulaReal = "";
			for (int a = 0; a < templateSelectValueArray.length; a++) {
				String templateSelectValue = templateSelectValueArray[a];
				String expressionTextValue = expressionTextValueArray[a];
				String rightFormulaShow = getRightFormulaShow(templateSelectValue,
						resolveValidationFormulaLeft(expressionTextValue));
				String rightFormulaReal = getCellId(templateSelectValue,
						resolveValidationFormulaLeft(expressionTextValue));
				formulaShow += rightFormulaShow + "+";
				formulaReal += rightFormulaReal + "+";
			}
			formulaShow = leftFormulaShow + getFormulaSymbol(valudationFormulaSymbol)
					+ formulaShow.substring(0, formulaShow.length() - 1);
			formulaReal = leftFormulaReal + getFormulaSymbol(valudationFormulaSymbol)
					+ formulaReal.substring(0, formulaReal.length() - 1);
			RptTableValidationFormula rptTableValidationFormula = new RptTableValidationFormula();
			rptTableValidationFormula.setShowTableValidationFormula(formulaShow.toUpperCase());
			if (!(formulaReal.contains(">") || formulaReal.contains("<"))) {
				String strA = formulaReal.replaceFirst("=", "==");
				rptTableValidationFormula.setRealTableValidationFormula(strA);
			} else {
				rptTableValidationFormula.setRealTableValidationFormula(formulaReal.replaceFirst("=", "=="));
			}
			saveRptValidation(rptTableValidationFormula, templateId);
		} else if (relationSelectName.equals("2")) {
			String leftFormulaShow = resolveValidationFormulaLeft(leftTableValidationFormulaStr);
			String leftFormulaReal = getCellId(templateId, leftFormulaShow);
			String formulaShow = "";
			String formulaReal = "";
			for (int a = 0; a < templateSelectValueArray.length; a++) {
				String templateSelectValue = templateSelectValueArray[a];
				String expressionTextValue = expressionTextValueArray[a];
				String rightFormulaShow = getRightFormulaShow(templateSelectValue,
						resolveValidationFormulaLeft(expressionTextValue));
				String rightFormulaReal = getCellId(templateSelectValue,
						resolveValidationFormulaLeft(expressionTextValue));
				formulaShow = leftFormulaShow + getFormulaSymbol(valudationFormulaSymbol) + rightFormulaShow;
				formulaReal = leftFormulaReal + getFormulaSymbol(valudationFormulaSymbol) + rightFormulaReal;
				RptTableValidationFormula rptTableValidationFormula = new RptTableValidationFormula();
				rptTableValidationFormula.setShowTableValidationFormula(formulaShow.toUpperCase());
				if (!(formulaReal.contains(">") || formulaReal.contains("<"))) {
					String strA = formulaReal.replaceFirst("=", "==");
					rptTableValidationFormula.setRealTableValidationFormula(strA);
				} else {
					rptTableValidationFormula.setRealTableValidationFormula(formulaReal.replaceFirst("=", "=="));
				}
				saveRptValidation(rptTableValidationFormula, templateId);
			}
		} else if (relationSelectName.equals("3")) {
		} else {
		}
	}

	//解析这种表达式（A1:A20）,这种表达式表示，从A1到A20分别大于....，将这种表达式转换成字符串集合[A1,A2,A3,A4.....]
	public List<String> resolveAValidationFormula(String validationFormula) {
		String[] validationFormulaArray = validationFormula.split(":");
		//判断数组长度，大于2的话，说明至少有两个：符号，现在表达式只支持一个，所以返回NULL
		if (validationFormulaArray.length > 2) {
			return null;
		} else {
			String letterStart = validationFormulaArray[0];
			String letterEnd = validationFormulaArray[1];
			String numStrStart = letterStart.replaceAll("[a-zA-Z]", "");
			String letterStrStart = letterStart.replaceAll("[0-9]", "");
			String numStrEnd = letterEnd.replaceAll("[a-zA-Z]", "");
			//String letterStrEnd = letterEnd.replaceAll("[0-9]", "");
			List<String> strList = new ArrayList<String>();
			if (!letterStrStart.equals("letterStrEnd")) {
				return null;
			} else {
				for (int a = Integer.parseInt(numStrStart); a < Integer.parseInt(numStrEnd) + 1; a++) {
					String str = letterStrStart + a;
					strList.add(str);
				}
				return strList;
			}
		}
	}

	//解析这种表达式（A1~A20）,这种表达式表示，从A1相加到A20，将这种表达式解析成A1+A2+A3+A4.....,返回字符串
	public String resolveValidationFormulaLeft(String validationFormula) {
		//先判断有没有破浪线，没有直接返回，有的话才进行下一步操作
		if (validationFormula.indexOf("~") > 0) {
			String waveString[] = validationFormula.split("~");
			for (int a = 0; a < waveString.length; a++) {
				if (a + 1 == waveString.length) {
					break;
				}
				String startStr = getNumStart(waveString[a]);
				String endStr = getNumEnd(waveString[a + 1]);
				//判断字母是否相同
				String numStrStatr = startStr.replaceAll("[a-zA-Z]", "");
				String letterStrStatr = startStr.replaceAll("[0-9]", "");
				String numStrEnd = endStr.replaceAll("[a-zA-Z]", "");
				String letterStrEnd = endStr.replaceAll("[0-9]", "");
				if (letterStrStatr.equals(letterStrEnd)) {
					//算出开始和结束的差值，并进行循环，补全破浪线中的字母数字
					String middleStr = "";
					for (int b = Integer.parseInt(numStrStatr); b < Integer.parseInt(numStrEnd) + 1; b++) {
						middleStr += letterStrStatr + b + "+";
					}
					validationFormula = validationFormula.replace(startStr + "~" + endStr,
							middleStr.substring(0, middleStr.length() - 1));
				}

			}
			return validationFormula;
		} else {
			return validationFormula;
		}
	}

	//通过传入报表的ID和公式，得到一个只有单元格ID的公式
	public String getCellId(String templateId, String formula) {
		String[] letterArray = formula.split("[\\+\\-\\*\\=\\>\\</]");
		for (int a = 0; a < letterArray.length; a++) {
			String cellId = reportTemplateValidationService.getCellId(templateId, letterArray[a]);
			formula = formula.replaceFirst(letterArray[a], cellId);
		}
		return formula;
	}

	//通过传入报表的ID和公式，得到一个展现公式，例如党员基本情况表.A1+党员基本情况表.A2+党员基本情况表.A3.....
	public String getRightFormulaShow(String templateId, String formula) {
		RptTemplate rptTemplate = reportTemplateManageService.getAnRptTemplate(templateId);
		String[] letterArray = formula.split("[\\+\\-\\*\\=\\>\\</]");
		for (int a = 0; a < letterArray.length; a++) {
			formula = formula.replaceFirst(letterArray[a], rptTemplate.getName() + "." + letterArray[a]);
		}
		return formula;
	}

	//得到公式中间连接符
	public String getFormulaSymbol(String valudationFormulaSymbol) {
		switch (valudationFormulaSymbol) {
		case "1":
			return "=";
		case "2":
			return ">";
		case "3":
			return "<";
		case "4":
			return ">=";
		case "5":
			return "<=";
		default:
			return "";
		}
	}

	//校核公式需要用到的新方法
	//通过报表的ID，获取行/列/单元格/表间
	@ResponseBody
	@RequestMapping(value = "/getValidationRowByTemplateId/{templateId}/{type}")
	public Map getZongkongCondByTemplateId(@PathVariable String templateId, @PathVariable String type,
			HttpServletRequest request, @RequestParam(value = "pageNum") int pageNum,
			@RequestParam(value = "pageSize") int pageSize, @RequestParam(value = "sortname") String sortname,
			@RequestParam(value = "sortorder") String sortorder) {
		Criteria cnd = PropertyFilter.cri(request);
		//1:行，2：列，3：单元格,4:表间
		if (type.equals("1")) {
			cnd.where().and("TEMPLATE_ID", "=", templateId).and("validationType", "=",
					ValidationType.inner_row.getKey());
		} else if (type.equals("2")) {
			cnd.where().and("TEMPLATE_ID", "=", templateId).and("validationType", "=",
					ValidationType.inner_col.getKey());
		} else if (type.equals("3")) {
			cnd.where().and("TEMPLATE_ID", "=", templateId).and("validationType", "=",
					ValidationType.inner_cell.getKey());
		} else if (type.equals("4")) {
			cnd.where().and("TEMPLATE_ID", "=", templateId).and("validationType", "=",
					ValidationType.outer_cell.getKey());
		} else {

		}
		cnd.getOrderBy().asc("VALIDATION_DESC");

		return reportTemplateValidationService.getValidationByTypeLigerUi(cnd, pageNum, pageSize);
	}

	@RequestMapping(value = "/preCreateRowValidation/{templateId}")
	public String preCreateRowValidation(@PathVariable String templateId, Model model) {
		model.addAttribute("templateId", templateId);
		return "report-design/report-template-validation-hang-input";
	}

	@RequestMapping(value = "/preCreateColValidation/{templateId}")
	public String preCreateColValidation(@PathVariable String templateId, Model model) {
		model.addAttribute("templateId", templateId);
		return "report-design/report-template-validation-lie-input";
	}

	@RequestMapping(value = "/preCreateCellValidation/{templateId}")
	public String preCreateCellValidation(@PathVariable String templateId, Model model) {
		model.addAttribute("templateId", templateId);
		return "report-design/report-template-validation-cell-input";
	}

	@RequestMapping(value = "/preCreateRptValidation/{templateId}/{annualId}")
	public String preCreateRptValidation(@PathVariable String templateId, @PathVariable String annualId, Model model) {
		model.addAttribute("templateId", templateId);
		model.addAttribute("annualId", annualId);
		return "report-design/report-template-validation-rpt-input";
	}

	@RequestMapping(value = "/saveRowValidation", method = RequestMethod.POST)
	public String saveRowValidation(@RequestParam String templateId, @RequestParam String validationType,
			@RequestParam String validationJspWrite, @RequestParam String validationDesc, Model model) {
		validationJspWrite = validationJspWrite.replaceAll("\r|\n|\t", "");
		List<String> strList = rowValidationChange(validationJspWrite);
		RptTemplateValidation rptTemplateValidation = new RptTemplateValidation();
		rptTemplateValidation.setValidationFormula(strList.get(1));
		rptTemplateValidation.setValidationFormulaShow(strList.get(0));
		rptTemplateValidation.setTemplateId(templateId);
		rptTemplateValidation.setValidationDesc(Integer.parseInt(validationDesc));
		rptTemplateValidation.setValidationType(validationType);
		reportTemplateValidationService.insert(rptTemplateValidation);
		model.addAttribute("message", "保存成功");
		return "report-design/report-template-validation-hang-input";
	}

	@RequestMapping(value = "/saveColValidation", method = RequestMethod.POST)
	public String saveColValidation(@RequestParam String templateId, @RequestParam String validationType,
			@RequestParam String validationJspWrite, @RequestParam String validationDesc, Model model) {
		validationJspWrite = validationJspWrite.replaceAll("\r|\n|\t", "");
		List<String> strList = colValidationChange(validationJspWrite);
		RptTemplateValidation rptTemplateValidation = new RptTemplateValidation();
		rptTemplateValidation.setValidationFormula(strList.get(0));
		rptTemplateValidation.setValidationFormulaShow(strList.get(1));
		rptTemplateValidation.setTemplateId(templateId);
		rptTemplateValidation.setValidationDesc(Integer.parseInt(validationDesc));
		rptTemplateValidation.setValidationType(validationType);
		reportTemplateValidationService.insert(rptTemplateValidation);
		model.addAttribute("message", "保存成功");
		return "report-design/report-template-validation-lie-input";
	}

	@RequestMapping(value = "/saveCellValidation", method = RequestMethod.POST)
	public String saveCellValidation(@RequestParam String templateId, @RequestParam String validationType,
			@RequestParam String validationJspWrite, @RequestParam String validationDesc, Model model) {
		validationJspWrite = validationJspWrite.replaceAll("\r|\n|\t", "");
		List<String> strList = cellValidationChange(validationJspWrite, templateId);
		if (strList.get(0).equals("公式错误")) {
			model.addAttribute("message", "公式错误");
		} else {
			RptTemplateValidation rptTemplateValidation = new RptTemplateValidation();
			rptTemplateValidation.setValidationFormula(strList.get(0));
			rptTemplateValidation.setValidationFormulaShow(strList.get(1));
			rptTemplateValidation.setTemplateId(templateId);
			rptTemplateValidation.setValidationDesc(Integer.parseInt(validationDesc));
			rptTemplateValidation.setValidationType(validationType);
			reportTemplateValidationService.insert(rptTemplateValidation);
			model.addAttribute("message", "保存成功");
		}
		return "report-design/report-template-validation-cell-input";
	}

	public void saveRptValidation(RptTableValidationFormula rptTableValidationFormula, String templateId) {
		RptTemplateValidation rptTemplateValidation = new RptTemplateValidation();
		rptTemplateValidation.setValidationFormula(rptTableValidationFormula.getRealTableValidationFormula());
		rptTemplateValidation.setValidationFormulaShow(rptTableValidationFormula.getShowTableValidationFormula());
		rptTemplateValidation.setTemplateId(templateId);
		rptTemplateValidation.setValidationType("4");
		reportTemplateValidationService.insert(rptTemplateValidation);
	}

	@ResponseBody
	@RequestMapping(value = "/deleteTemplateValidationById/{validationId}", method = RequestMethod.POST)
	public String deleteTemplateConditionById(@PathVariable String validationId) {
		try {
			reportTemplateValidationService.deletaValidationById(validationId);
			return "success";
		} catch (Exception e) {
			return "false";
		}
	}

	@ResponseBody
	@RequestMapping(value = "/onloadValidationFormual/{templateId}/{annualId}", method = RequestMethod.POST)
	public List<String> onloadValidationFormual(@PathVariable String templateId, @PathVariable String annualId) {
		List<String> dataList = new ArrayList<String>();
		try {
			reportValidationFormulaOnloadService.onloadValidationCellFormula(templateId);
			reportValidationFormulaOnloadService.onloadReportBetweenValidationFormual(templateId, annualId);
			dataList.add("重新生成公式成功");
			return dataList;
		} catch (Exception e) {
			dataList.add("重新生成公式失败");
			return dataList;
		}
	}
}
